package com.huiquan.homoionym.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.huiquan.homoionym.domain.HomoionymSearchTerms;
import com.huiquan.homoionym.domain.HomoionymSearchTermsSub;
import com.huiquan.framework.base.EnhancedBaseDao;
import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class HomoionymSearchTermsSubDao extends EnhancedBaseDao<HomoionymSearchTermsSub> {

	@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;

	public static final String space = "HOMOIONYM_SEARCH_TERMS_SUB.";

	public HomoionymSearchTermsSubDao() {
		super(space);
	}

	public void delete(HomoionymSearchTerms homoionymSearchTerms, Long userId) {
		Map<String, Object> param = new HashMap<>();
		param.put("uid", userId);
		param.put("pid", homoionymSearchTerms.getId());
		
		this.update(space + "DELETE", param, sqlMapClient);
	}

	public List<HomoionymSearchTermsSub> retrieveListByPid(Long pid) {
		Map<String, Object> param = new HashMap<>();
		param.put("pid", pid);
		return this.retrieveList(param);
	}

	public void batchInsert(Long id, List<String> add, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("pid", id);
		param.put("words", add);
		param.put("uid", uid);
		this.insert(space + "BATCH_INSERT", param, sqlMapClient);
	}

	public void batchDelete(Long id, List<String> del, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("pid", id);
		param.put("words", getWordsByList(del));
		param.put("uid", uid);
		this.insert(space + "DELETE", param, sqlMapClient);
	}

	public void batchUpdate(Long id, List<String> upd, Long uid) {
		Map<String, Object> param = new HashMap<>();
		param.put("pid", id);
		param.put("words", getWordsByList(upd));
		param.put("uid", uid);
		
		this.update(space + "UPDATE", param, sqlMapClient);
	}

	private String getWordsByList(List<String> list) {
		StringBuilder words = new StringBuilder();
		words.append("(");
		for(String word:list) {
			words.append("'");
			words.append(word);
			words.append("',");
		}
		words.deleteCharAt(words.length()-1);
		words.append(")");
		return words.toString();
	}
	
	
}
